package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.SetmeatelDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class setmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmeatelDishMapper setmealdishService;
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long setmealId = setmeal.getId();
        for (SetmealDish setmealDish : setmealDishes) {
           setmealDish.setSetmealId(setmealId);
        }
        setmealdishService.insertBatch(setmealDishes);
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
       PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
    Page<SetmealVO>page=   setmealMapper.getpage(setmealPageQueryDTO);
    return new PageResult(page.getTotal(),page.getResult());

    }

//    @Override
//    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
//        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
//    Page<SetmealVO> page= setmealMapper.getpage(setmealPageQueryDTO);
//      return new PageResult(page.getTotal(),page.getResult());
//    }

    @Override
    public void updateStatus(Integer status,Long id) {
     //  Setmeal setmeal= setmealMapper.selectBy(id);
        Setmeal setmeal=new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
       setmealMapper.updateStatus(setmeal);
    }

    @Override
    public SetmealVO selectById(Long id) {
     Setmeal setmeal=setmealMapper.selectBys(id);
     List<SetmealDish>list=setmealdishService.selectByid(id);
//       SetmealVO setmealVO= setmealMapper.selectBy(id);
//        return setmealVO;
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        Long setmealDTOId = setmealDTO.getId();
        setmealdishService.delete(setmealDTOId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTOId);
        }
        //SetmealDish setmealDish=new SetmealDish();
        setmealdishService.insertBatch(setmealDishes);


    }

    @Override
    public void deleteIds(List<Long> ids) {
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.selectBy(id);
            if (setmealVO.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

        }
        setmealMapper.deleteByIds(ids);
        for (Long id : ids) {
            setmealdishService.deleteBySetmealId(id);
          //  setmealDishMapper.deleteBySetmealId(setmealId);
        }
        

    }
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
